package com.miplus.ccrm.core.service.commission;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.core.controller.vo.commission.CaseStatisticsReCalVo;
import com.miplus.ccrm.core.controller.vo.commission.CaseStatisticsReqVo;
import com.miplus.ccrm.core.controller.vo.commission.CaseStatisticsResVo;
import com.miplus.ccrm.core.controller.vo.credit.CrdCaseReqVo;
import com.miplus.ccrm.core.controller.vo.credit.CrdCaseResVo;
import com.miplus.ccrm.core.pojo.ConCustomer;
import com.miplus.ccrm.core.pojo.FiCasesRepaymentStatistics;
import com.miplus.ccrm.core.service.ExportService;
import com.miplus.ccrm.core.service.commission.FiCasesRepaymentStatisticsService;
import com.miplus.ccrm.core.mapper.FiCasesRepaymentStatisticsMapper;
import com.miplus.ccrm.core.service.crd.FiImportHistService;
import com.miplus.ccrm.core.utils.EnumFiCaseStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;

/**
* @author wangzl
* @description 针对表【fi_cases_repayment_statistics(在委案件每日还款统计表)】的数据库操作Service实现
* @createDate 2025-03-11 15:30:06
*/
@Service
public class FiCasesRepaymentStatisticsServiceImpl extends MPJBaseServiceImpl<FiCasesRepaymentStatisticsMapper, FiCasesRepaymentStatistics>
    implements FiCasesRepaymentStatisticsService{
    @Resource
    private ExportService exportService;
    @Resource
    private FiImportHistService fiImportHistService;

    @Override
    public IPage<CaseStatisticsResVo> getCaseRepayStatistics(CaseStatisticsReqVo para)
    {
        return selectJoinListPage(new Page<>(para.getPageNum(),para.getPageSize()),
                CaseStatisticsResVo.class,
                new MPJLambdaWrapper<FiCasesRepaymentStatistics>()
                        .leftJoin(ConCustomer.class, ConCustomer::getCustomernumber, FiCasesRepaymentStatistics::getCustomernumber)
                        .in(para.getSendtodept()!=null,FiCasesRepaymentStatistics::getSendtodept,para.getSendtodept())
                        .in(para.getSendtousr()!=null,FiCasesRepaymentStatistics::getSendtousr,para.getSendtousr())
                        .eq(StringUtils.isNotEmpty(para.getCreditcaseid()),FiCasesRepaymentStatistics::getCreditcaseid,para.getCreditcaseid())
                        .ge(StringUtils.isNotEmpty(para.getStartDate()),FiCasesRepaymentStatistics::getStatisticsDate,para.getStartDate())
                        .le(StringUtils.isNotEmpty(para.getEndDate()),FiCasesRepaymentStatistics::getStatisticsDate,para.getEndDate())
                        .selectAll(FiCasesRepaymentStatistics.class)
                        .selectAs( ConCustomer::getCustname, CaseStatisticsResVo::getCustomername)
                        .selectAs("fun_getnames('COLLECT.STATUS',t.status)",CaseStatisticsResVo::getStatusdesc)
                        .selectAs("(select grpname from sys_grp where grpid = t.sendtodept)", CaseStatisticsResVo::getSendtodeptname)
                        .selectAs("(select name from sys_user where usrid = t.sendtousr)",CaseStatisticsResVo::getSendtousrname)
                        .orderByDesc(FiCasesRepaymentStatistics::getStatisticsDate)
                );
    }
    @Override
    public CmdResVo reCalCaseStatistics(String usrid, CaseStatisticsReCalVo para)
    {

        return new CmdResVo(1,"后台运行中");
    }

    @Override
    public CmdResVo saveAsExcel(String usrid, CaseStatisticsReqVo paras, HttpServletResponse response) {

        int pagesize = paras.getPageSize();
        if (pagesize > JPubUtils.exportMaxCount) {
            paras.setPageSize(JPubUtils.exportMaxCount);
        }

        IPage<CaseStatisticsResVo> pages=getCaseRepayStatistics(paras);
        ArrayList arr=new ArrayList();

        String exptempid=JPubUtils.getExpTempID(paras.getExpfilename(),"回款统计表");
        String filename=JPubUtils.getExpFileName(
                paras.getExpfilename(),exptempid
        );

        pages.convert(u->{
            HashMap map=JPubUtils.toExportMap(u);
            if(map!=null) arr.add(map);
            return u;
        });
        CmdResVo resVo=exportService.ToExcel(
                response,
                arr,
                paras.getExpidlist(),
                paras.getExpnamelist(),
                filename,
                usrid
        );

        int okcount=resVo.getCount();
        if(okcount>0){
            fiImportHistService.AddExportHist(
                    resVo.getKeyId(),
                    usrid,
                    exptempid,
                    filename,
                    okcount
            );
        }

        return resVo;
    }
}




